package com.lianyu.wangyiyun.model;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Chen Yuguang
 * @date 2021/5/15 5:44
 */
public class CloudMusic {

    private Result result;

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public static class Result {

        private List<Songs> songs;

        public List<Songs> getSongs() {
            return songs;
        }

        public void setSongs(List<Songs> songs) {
            this.songs = songs;
        }

        public static class Songs {

            private String name;
            private long id;
            private List<Ar> ar;
            private Al al;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public long getId() {
                return id;
            }

            public void setId(long id) {
                this.id = id;
            }

            public List<Ar> getAr() {
                return ar;
            }

            public void setAr(List<Ar> ar) {
                this.ar = ar;
            }

            public Al getAl() {
                return al;
            }

            public void setAl(Al al) {
                this.al = al;
            }

            public static class Al {

                private String picUrl;

                public String getPicUrl() {
                    return picUrl;
                }

                public void setPicUrl(String picUrl) {
                    this.picUrl = picUrl;
                }

            }

            public static class Ar {

                private String name;

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

            }

        }

    }

    /**
     * 将获取的云音乐信息转换为本地音乐信息
     */
    public List<Mp3Info> cloud2local() {
        List<Mp3Info> mp3Infos = new ArrayList<>();
        if (getResult() != null) {
            List<Result.Songs> songs = getResult().songs;
            if (songs != null) {
                for (Result.Songs song : songs) {
                    Mp3Info mp3Info = new Mp3Info();
                    if (song.id != 0) {
                        mp3Info.setId(song.id);
                        mp3Info.setUrl("https://music.163.com/song/media/outer/url?id=" + song.id + ".mp3");
                    }
                    if (song.name != null) {
                        mp3Info.setTitle(song.name);
                    }
                    if (song.ar != null) {
                        mp3Info.setArtist("");
                        for (Result.Songs.Ar ar : song.ar) {
                            mp3Info.setArtist(mp3Info.getArtist() + ar.name + "，");
                        }
                        mp3Info.setArtist(mp3Info.getArtist().substring(0, mp3Info.getArtist().length() - 1));
                    }
                    if (song.al != null) {
                        if (song.al.picUrl != null) {
                            mp3Info.setAlbum(song.al.picUrl);
                        }
                    }
                    mp3Infos.add(mp3Info);
                }
            }
        }
        return mp3Infos;
    }

}
